package com.example.firstapp;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class DataStorageActivity extends AppCompatActivity {

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private final static String SHARED_PREFERENCES_NAME = "sp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_data_storage);

        findView();
    }

    private void findView() {
//        textView1 = findViewById(R.id.activity_sharedperference_textview1);
//        textView1.setOnClickListener(this);
        //获取SharedPreferences对象
        sharedPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE);
    }

    public void innerWrite(View view) throws Exception {
        FileOutputStream fos = openFileOutput("inner.txt", MODE_PRIVATE);
        fos.write("Hello, inner storage!".getBytes());
        fos.close();
        Toast.makeText(this, "写入成功！", Toast.LENGTH_SHORT).show();
    }

    public void innerRead(View view) throws Exception {
        FileInputStream fis = openFileInput("inner.txt");
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        Toast.makeText(this, new String(buffer), Toast.LENGTH_SHORT).show();
    }

    public void outerWrite(View view) throws Exception {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)){
            // 写入数据
            File path = Environment.getExternalStorageDirectory();
            File file = new File(path, "outer.txt");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write("Hello, outer storage!".getBytes());
            fos.close();
            Toast.makeText(this, "外部写入成功！", Toast.LENGTH_SHORT).show();
        }
    }

    public void outerRead(View view) throws Exception {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)){
            // 读取数据
            File path = Environment.getExternalStorageDirectory();
            File file = new File(path, "outer.txt");
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            Toast.makeText(this, "外部读取："+ new String(buffer), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * SharedPreferences存数值
     */
    public void spWrite(View view) {
        // 存数值时 首先获取SharedPreferences.Editor对象
        editor = sharedPreferences.edit();
        //存String类型
        editor.putString("name", "ZhangSan");
        //存Int类型
        editor.putInt("age",22);
        //存StringSet类型
        Set<String> hobbySet = new HashSet<>();
        hobbySet.add("下棋");
        hobbySet.add("游泳");
        hobbySet.add("登山");
        editor.putStringSet("hobby", hobbySet);
        // 最终提交 apply异步提交
        editor.apply();
    }

    /**
     * SharedPreferences取数值
     */
    public void spRead(View view) {
        int age = sharedPreferences.getInt("age", 0);
        String name = sharedPreferences.getString("name", "");
        Set<String> hobbySet = sharedPreferences.getStringSet("hobby", null);
        Log.d("TAG", "SharedPreferences取值 Int类型 ----:" + age);
        Log.d("TAG", "SharedPreferences取值 String类型 ----:" + name);
        Log.d("TAG", "SharedPreferences取值 Set类型 ----:" + hobbySet);
        if (null != hobbySet) {
            for (String s1 : hobbySet) {
                Log.d("TAG", "SharedPreferences取值 Set类型 ----:" + s1);
            }
        }
    }
}